//
//  EditTableViewCell.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/28.
//

#import "EditTableViewCell.h"

@interface EditTableViewCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *avatarImageV;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UIImageView *arrowImageV;
@property (nonatomic, strong) UITextField *nameTF;

@end

@implementation EditTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.avatarImageV];
    [self.contentView addSubview:self.detailLabel];
    [self.contentView addSubview:self.nameTF];
    [self.contentView addSubview:self.arrowImageV];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
    }];
    [self.arrowImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.size.mas_equalTo(CGSizeMake(8, 12));
        make.centerY.mas_equalTo(0);
    }];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.arrowImageV.mas_leading).offset(-10);
        make.centerY.mas_equalTo(0);
    }];
    [self.avatarImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.arrowImageV.mas_leading).offset(-10);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(40);
    }];
    [self.nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.arrowImageV.mas_leading).offset(-10);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(30);
    }];
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = kRGB(0x333333);
        _titleLabel.font = kFontRegula(14);
        [_titleLabel sizeToFit];
    }
    return _titleLabel;
}
- (UIImageView *)arrowImageV {
    if (!_arrowImageV) {
        _arrowImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mine_arrow"]];
        _arrowImageV.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _arrowImageV;
}
- (UIImageView *)avatarImageV {
    if (!_avatarImageV) {
        _avatarImageV = [[UIImageView alloc] init];
        _avatarImageV.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageV.layer.cornerRadius = 20;
        _avatarImageV.layer.masksToBounds = YES;
    }
    return _avatarImageV;
}
- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = kFontRegula(14);
        _detailLabel.textColor = kRGB(0x666666);
        [_detailLabel sizeToFit];
    }
    return _detailLabel;
}
- (UITextField *)nameTF {
    if (!_nameTF) {
        _nameTF = [[UITextField alloc] initWithFrame:CGRectMake(50, 15, 100, 30)];
        _nameTF.textColor = kRGB(0x666666);
        _nameTF.font = kFontRegula(14);
        _nameTF.borderStyle = UITextBorderStyleNone;
        _nameTF.textAlignment = NSTextAlignmentRight;
        _nameTF.backgroundColor = [UIColor clearColor];
        _nameTF.returnKeyType = UIReturnKeyDone;
        _nameTF.delegate = self;
        @weakify(self)
        [_nameTF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
         @strongify(self)
            if (x.length > 16) {
                self.nameTF.text = [x substringToIndex:16];
            }
        }];
    }
    return _nameTF;
}

- (void)setModel:(MineModel *)model {
    _model = model;
    self.titleLabel.text = model.title;
    if (model.type == MineType_avatar) {
        self.nameTF.hidden = self.detailLabel.hidden = YES;
        self.avatarImageV.hidden = NO;
        [self.avatarImageV sd_setImageWithURL:[NSURL URLWithString:model.detailStr] placeholderImage:[UIImage imageNamed:@"avatar"]];
    }else if (model.type == MineType_phone) {
        self.nameTF.hidden = self.avatarImageV.hidden = YES;
        self.detailLabel.hidden = NO;
        self.detailLabel.text = model.detailStr;
    }else {
        self.avatarImageV.hidden = self.detailLabel.hidden = YES;
        self.nameTF.hidden = NO;
        self.nameTF.text = model.detailStr;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.nameTF resignFirstResponder];
    if (self.updateNickName) {
        self.updateNickName(self.nameTF.text);
    }
    return YES;
}

@end
